#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/3/6 0006 15:18
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : prepross
import pandas as pd
import numpy as np


def get_mean_age(data):
    age = data['Age']
    null_age = pd.isnull(age)
    rel_age = age[null_age == False]  # 返回所有非空age
    avge = round((sum(rel_age) / len(rel_age)), 2)  # 返回平均值
    return round(age.mean(), 2)


def get_mean_price(data):
    pclass = [1, 2, 3]
    price = {}
    for x in pclass:
        t = data['Fare'][data['Pclass'] == x].mean()
        price[x] = t
    return price


def not_null(col):
    is_null = pd.isnull(col)
    all_null = col[is_null]
    return len(all_null)


def reclass(row):
    pclass = row['Pclass']
    if pd.isnull(pclass):
        return 'Unknown'
    elif pclass == 1:
        return 'First'
    elif pclass == 2:
        return 'Second'
    elif pclass == 3:
        return 'Third'


def split_age(row):
    age = row['Age']
    if pd.isnull(age):
        return 'Unknown'
    elif age <= 18:
        return 'Child'
    elif age >= 45:
        return 'Old'
    else:
        return 'Adult'


def run(data):
    # age=get_mean_age(data)  #平均年龄
    # print(age)
    # price=get_mean_price(data)  #船舱均价
    # print(price)
    # chance=data.pivot_table(index='Pclass',values='Survived',aggfunc=np.mean)      #船舱等级获救几率
    # print(chance)
    # page=data.pivot_table(index='Pclass',values='Age',aggfunc=np.mean)
    # print(page)
    # place=data.pivot_table(index='Embarked',values=['Fare','Survived','Age'],aggfunc=np.sum)    #多指标
    # print(place)
    # drop_row_data=data.dropna(axis=1)   #删除有缺省值的列
    # print(drop_row_data)
    # drop_col_data=data.dropna(axis=0,subset=['Age','Cabin'])  #删除指定列有缺省值的行
    # print(drop_col_data)
    # print(data.loc[9,'Age'])        #定位  输出第10行年龄
    # sort_age=data.sort_values('Age',ascending=False)    #年龄排序
    # new_index=sort_age.reset_index(drop=True)           #重写索引
    # print(sort_age[:10])
    # print(new_index.loc[:9])
    # print(data.apply(not_null))             #自定义函数
    # print(data.apply(reclass,axis=1)[:10])
    age_labels = data.apply(split_age, axis=1)
    data['age_labels'] = age_labels
    age_survival = data.pivot_table(index='age_labels', values="Survived")
    print(age_survival)


if __name__ == '__main__':
    data = pd.read_csv('name_list.csv')
    run(data)
